package com.junyu.controller;

import com.junyu.common.Result;
import com.junyu.pojo.entity.LoginLog;
import com.junyu.pojo.vo.LoginStatistics;
import com.junyu.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日志Controller
 */
@RestController
@Api(description = "日志Controller")
@CrossOrigin
public class LogController {
    @Autowired
    private LogService logService;

    /**
     * 新增日志
     *
     * @param loginLog
     * @return Result 用户退出登录响应对象
     */

    @PostMapping("/saveLog")
    @ApiOperation(value = "新增日志")
    public Result saveLog(@RequestBody LoginLog loginLog) {
        this.logService.saveLog(loginLog);
        return Result.ok();
    }

    /**
     * 查询上一次日志接口
     * @param userName 用户名
     * @return LoginLog 登录日志对象
     */
    @GetMapping("/findLogByUserName")
    @ApiOperation(value = "查询上一次日志接口")
    public Result<LoginLog> findLogByUserName(String userName) {
        LoginLog loginLog = this.logService.findLogByUserName(userName);
        return Result.ok(loginLog);
    }


    /**
     * 近七日登录人次接口
     * @return List<LoginStatistics> 统计对象集合
     */
    @ApiOperation(value = "近七日登录人次接口")
    @ApiImplicitParam(name = "token", required = true, paramType = "header", dataType = "String")
    @GetMapping("/login/count")
    public Result<List<LoginStatistics>> loginCount() {
        List<LoginStatistics> loginStatistics = this.logService.loginCount();
        return Result.ok(loginStatistics);
    }
}
